<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class search_controller extends MY_Controller {
// Some code happens here
    public function __construct()
    {
        parent:: __construct();
        $this->load->helper("url");
	    $this->load->model('sanpham');
        $this->load->model('timkiem');
        $this->load->library('url');    
        $this->load->helper('html');
		$this->load->helper('text');
	    $this->load->library('parser');
        $this->load->library('pagination');
        $this->load->library('session');
        $this->load->library('currency');
    }
	
    public function render_post($data)
    {
        return $this->parser->parse('main/product_item_simp',$data,true);
    }
    
    public function fix_search_content($search_content)
    {
        $search_content = str_replace("'","",$search_content);
        return $search_content;
    }
    
    public function view_tooltip($data)
    {
        return $this->parser->parse('main/tooltip',$data,true);
    }
    
    public function tooltip($id)
    {
        $product_id = $id;
        $tooltip_db['id'] = $product_id;
        $tooltip_db['name'] = $this->sanpham->get_product_name($product_id);
        $tooltip_db['price'] = $this->sanpham->get_product_prices($product_id);
        for($i=1 ; $i <=11 ; $i++)
        {
            $tooltip_db['info'.$i] = $this->sanpham->get_product_info($product_id,$i);
        }
        $this->load->view('main/tooltip',$tooltip_db);
    }
        
    public function search_result_page($search_content , $search_type , $search_price_min , $search_price_max ,$offset = 0)
    {
        $newdata = array(
                   'search_content'  => $search_content,
                   'search_type'     => $search_type,
                   'search_price_min' => $search_price_min,
                   'search_price_max' => $search_price_max,
               );
        $this->session->set_userdata($newdata);
		//var_dump($this->session->userdata);
        $this->post['html']='';        
		$config['base_url'] = site_url('search_controller/search_result_page/');
        $config['total_rows'] = $this->timkiem->get_row($search_content,$search_type,$search_price_min,$search_price_max);
	    $config['per_page'] = 20;
        $config['num_links'] = 10000;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';

		
	    $this->pagination->initialize($config);
	    $pdata['html'] = $this->pagination->create_links();
        $arrProducts = $this->timkiem->get_result_all($search_content,$search_type,$search_price_min,$search_price_max,'20',$offset);
        if($search_type == '-1' && $search_price_max == '-1' && $search_price_min == '-1')
        {
            $arrProducts = $this->timkiem->get_result_all_ev($search_content,'20',$offset);
        }
        else if($search_price_min == 0 && $search_price_max == 0)
		{
			$arrProducts = $this->timkiem->get_result_all_non_price($search_content,$search_type,'20',$offset);
		}
		else {
			$arrProducts = $this->timkiem->get_result_all($search_content,$search_type,$search_price_min,$search_price_max,'20',$offset);
		}
		foreach($arrProducts as $product)
        {   
            $data['id'] = $product->ID;
            $data['name'] = $product->TenSP;
            $data['gia'] =$this->currency->convert_to_vnd($product->Gia);
            $data['tooltip'] = $this->view_tooltip($data);
            $this->post['html'] .= $this->render_post($data);     
        }
		
        $html = $this->parser->parse('main/search_result',$pdata,true);
        $html2 = $this->parser->parse('home/content',$this->post,true);
        $this->post['html'] = $html2.$html; 
        echo $html2.$html; 
    }
    
    public function get_search_product_paging($search_content , $search_type , $search_price_min , $search_price_max , $offset = 0)
    {
     	$search_content = $this->session->userdata['search_content'];
		$search_type = $this->session->userdata['search_type'];
		$search_price_min = $this->session->userdata['search_price_min'];
		$search_price_max = $this->session->userdata['search_price_max'];
		
		$config['base_url'] = site_url('search_controller/get_search_product_paging/');
		$config['total_rows'] = $this->timkiem->get_row($search_content,$search_type,$search_price_min,$search_price_max);
	    $config['per_page'] = 20;
	    $config['cur_page'] = $offset/20;
        $config['num_links'] = 10000;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';
		
	    $this->pagination->initialize($config);
	    $pdata['html'] = $this->pagination->create_links();
        $this->post['html']='';
		if($search_type == '')
        {
            $arrProducts = $this->timkiem->get_result_all_ev($search_content,'20',$offset);
        }else if($search_price_min == 0 && $search_price_max == 0)
		{
			$arrProducts = $this->timkiem->get_result_all_non_price($search_content,$search_type,'20',$offset);
		}
		else 
		{
			$arrProducts = $this->timkiem->get_result_all($search_content,$search_type,$search_price_min,$search_price_max,'20',$offset);
		}
		foreach($arrProducts as $product)
        {   
            $data['id'] = $product->ID;
            $data['name'] = $product->TenSP;
            $data['gia'] = $product->Gia;
            $data['tooltip'] = $this->view_tooltip($data);
            $this->post['html'] .= $this->render_post($data);     
        }
       $html = $this->parser->parse('main/search_result',$pdata,true);
       $html2 = $this->parser->parse('home/content',$this->post,true);
       echo $html2.$html; 
}  
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */